package cn.kgc.tangcco.service.impl;

import cn.kgc.tangcco.dao.AccountMongoDao;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Account;
import cn.kgc.tangcco.service.AccountMongoServcie;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/29 11:40
 */
@Component
public class AccountMongoServcieImpl implements AccountMongoServcie {
    @Resource
    private AccountMongoDao accountMongoDao;


    @Override
    public ResponseText<Account> add(Account account) {
        if (accountMongoDao.findAccount(account.getMobile()) == null) {
            Account save = accountMongoDao.save(account);
            if (save == null) {
                return new ResponseText<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
            } else {
                return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), save);
            }
        }
        return new ResponseText<>(ResultCode.MOBILE_EXISTS.getCode(), ResultCode.MOBILE_EXISTS.getMsg(), null);
    }

    @Override
    public ResponseText<Account> login(String mobile) {
        Account account = accountMongoDao.findAccount(mobile);
        if (account != null) {
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), account);
        }
        return new ResponseText<>(ResultCode.LOGIN_FAILED.getCode(), ResultCode.LOGIN_FAILED.getMsg(), null);
    }
}
